package com.dsteiner.xelavos.client.domain;

import java.util.Date;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.dsteiner.widgets.client.DateUtilImpl;
import com.dsteiner.widgets.client.ObjectUtil;
import com.dsteiner.widgets.client.domain.ModelObject;
import com.dsteiner.widgets.client.domain.Searchable;
import com.dsteiner.widgets.client.domain.SearchableId;
import com.dsteiner.widgets.client.domain.SearchableProperty;
import com.dsteiner.widgets.client.domain.SearchableRefId;
import com.dsteiner.widgets.client.domain.SearchableValue;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
@Searchable
public class User extends ModelObject implements HasImage{

	private static final long serialVersionUID = 6686961552160206434L;
	
	public static final int GENDER_MALE = 1;
	public static final int GENDER_FEMALE = 2;

	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    @SearchableId
    private String id;
	
	@Persistent
    @SearchableProperty
    private String firstName;
	
	@Persistent
    @SearchableProperty
    private String lastName;
	
    @Persistent
    private Date joinDate;

    @Persistent
    private int entriesCount;

    @Persistent
	private String password;
    
    @Persistent
    private Boolean receiveReport;
    
    @Persistent
    private Boolean validated;
    
    @Persistent
    @SearchableValue
	private String email;
    
    @Persistent
    private int gender;

    @Persistent
    private Date birthday;

    @Persistent
    @SearchableRefId
	private String imageId;

    public User() {
	}

    public User(String firstName, String lastName, Date joinDate) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.joinDate = joinDate;
    }

    public boolean equals(Object obj) {
		if (obj == null)
			return false;
		if (!(obj instanceof User))
			return false;
		User other = (User) obj;
		return ObjectUtil.equals(getId(), other.getId());
	}

    public int getAge() {
    	return DateUtilImpl.getInstance().getAge(birthday);
	}
    public Date getBirthday() {
		return birthday;
	}
    public String getEmail() {
		return email;
	}

    public int getEntriesCount() {
		return entriesCount;
	} 
    public String getFirstName() {
        return firstName;
    } 

    public Boolean isReceiveReport() {
		return receiveReport;
	}
    public void setReceiveReport(Boolean receiveReport) {
		this.receiveReport = receiveReport;
	}
    
    public String getId(){
		return id;
	} 
    public String getImageId() {
		return imageId;
	} 

    public Date getJoinDate() {
        return joinDate;
    } 
    public String getLastName() {
        return lastName;
    }

    public String getPassword() {
		return password;
	}
	@Override
	public Date getRegisteredDate() {
		return joinDate;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	public void setEntriesCount(int entriesCount) {
		this.entriesCount = entriesCount;
	}

	public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
	public void setId(String id) {
		this.id = id;
	}
	
	public void setImageId(String imageId) {
		this.imageId = imageId;
	}

	public void setJoinDate(Date hireDate) {
        this.joinDate = hireDate;
    }
	
	public void setLastName(String lastName) {
        this.lastName = lastName;
    }

	public void setPassword(String password) {
		this.password = password;
	}

	public int getGender() {
		return gender;
	}
	public void setGender(int gender) {
		this.gender = gender;
	}
	public boolean isMale(){
		return gender == GENDER_MALE;
	}
	public boolean isFemale(){
		return gender == GENDER_FEMALE;
	}
	public Boolean getReceiveReport() {
		return receiveReport;
	}
	public Boolean isValidated() {
		return validated;
	}
	public Boolean getValidated() {
		return validated;
	}
	public void setValidated(Boolean validated) {
		this.validated = validated;
	}
	@Override
	public String toString() {
		return firstName + " " + lastName;
	}
}